using IRepository;
using IServices;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using YL.Core.Dto;
using YL.Core.Entity;
using YL.Core.Entity.Meter;
using YL.Utils.Extensions;
using YL.Utils.Json;
using YL.Utils.Pub;
using YL.Utils.Table;

namespace Services
{
    public class Meter_ClientInfoServices : BaseServices<ClientInfo>, IMeter_ClientInfoServices
    {
        private readonly IMeter_ClientInfoRepository _repository;
        private readonly ISys_rolemenuServices _rolemenuServices;
        private readonly ISys_menuServices _menuServices;
        private readonly SqlSugarClient _client;

        public Meter_ClientInfoServices(ISys_menuServices menuServices, ISys_rolemenuServices rolemenuServices, SqlSugarClient client, IMeter_ClientInfoRepository repository) : base(repository)
        {
            _repository = repository;
            _client = client;
            _rolemenuServices = rolemenuServices;
            _menuServices = menuServices;
        }

        public string PageList(Bootstrap.BootstrapParams bootstrap)
        {
            int totalNumber = 0;
            if (bootstrap.offset != 0)
            {
                bootstrap.offset = bootstrap.offset / bootstrap.limit + 1;
            }
            var depts = bootstrap.depts;
            if (depts == null)
                depts = new List<long>();

            var query = _client.Queryable<ClientInfo, Sys_user, Sys_user>
                ((s, c, u) => new object[] {
                   JoinType.Left,s.UserId==c.UserId,
                   JoinType.Left,s.ModifiedBy==u.UserId
                 })
                .Where((s, c, u) => depts.Contains(s.ParentId)).Select((s, c, u) => new
                {
                    ClientId = s.ClientId.ToString(),
                    ParentId = s.ParentId.ToString(),
                    s.ClientName,
                    s.Longtitude,
                    s.Latitude,
                    s.ClientArea,
                    s.Description,
                    s.UserId,
                    s.ModifiedBy,
                    CName = c.UserNickname,
                    UName = u.UserNickname,
                    s.CreateDate,
                    s.ModifiedDate
                }).MergeTable().Where((s) => s.ClientId != null).OrderBy(s=>s.ParentId).OrderBy(s=>s.ClientName);



            if (!bootstrap.search.IsEmpty())
            {
                query.Where((s) => s.ClientName.Contains(bootstrap.search));
            }

            if (!bootstrap.datemin.IsEmpty() && !bootstrap.datemax.IsEmpty())
            {
                query.Where(s => s.CreateDate > bootstrap.datemin.ToDateTimeB() && s.CreateDate <= bootstrap.datemax.ToDateTimeE());
            }
            if (bootstrap.order.Equals("desc", StringComparison.OrdinalIgnoreCase))
            {
                query.OrderBy($"MergeTable.{bootstrap.sort} desc");
            }
            else
            {
                query.OrderBy($"MergeTable.{bootstrap.sort} asc");
            }
            var list = query.ToPageList(bootstrap.offset, bootstrap.limit, ref totalNumber);
            return Bootstrap.GridData(list, totalNumber).JilToJson();
        }

        /// <summary>
        /// »ñÈ¡ÓÃ»§±í
        /// </summary>
        /// <param name="bootstrap"></param>
        /// <param name="DeptNo"></param>
        /// <returns></returns>
        public string SingleList(List<long> depts)
        {
            var query = _client.Queryable<ClientInfo>().Where(u=> depts.Contains(u.ParentId))
                 .Select((s) => new
                 {
                     ClientId = s.ClientId.ToString(),
                     s.ClientName
                 }).ToList();

            return query.JilToJson();
        }
    }
}